//
//  WBComposeViewController.swift
//  新浪微博
//
//  Created by 张甜甜 on 2017/12/11.
//  Copyright © 2017年 sweet. All rights reserved.
//

import UIKit
import SVProgressHUD
//撰写微博控制器
/*
 加载视图控制器的时候，如果xib和控制器同名11
 默认的构造函数会默认加载Xib
 */
class WBComposeViewController: UIViewController {
    //文本编辑视图
    //底部工具栏
    private lazy var textView:WBComposeTextView = WBComposeTextView()
    private lazy var toolBar:UIView = UIView()
    private lazy var placeHoderLabel:UILabel = UILabel()
    private lazy var postButton:UIButton = UIButton(type: .custom)
    lazy var emoticonView:CZEmoticoInputView = CZEmoticoInputView.inputView { [weak self](emoticon) in
        self?.textView .insertEmoticon(em: emoticon)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        
        setUPUi()
        //监听键盘通知-UIWindow
        //通知是一对多 如果其他控件监听当前视图的通知不影响
        //如果使用代理 其他空间就无法使用代理监听通知
        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyBoarHidden), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    @objc private func keyBoardShow(n:NSNotification){
        UIView.animate(withDuration: 1) {
            self.toolBar.frame  = CGRect(x: self.toolBar.frame.origin.x, y: UIScreen.cz_screenHeight()-258-45, width: self.toolBar.frame.size.width, height: self.toolBar.frame.size.height)
        }
    }
    @objc private func keyBoarHidden(){
    UIView.animate(withDuration: 1) {
    self.toolBar.frame  = CGRect(x: self.toolBar.frame.origin.x, y: UIScreen.cz_screenHeight()-45, width: self.toolBar.frame.size.width, height: self.toolBar.frame.size.height)
     }
    }
    @objc private func close(){
        dismiss(animated: true, completion: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func viewWillDisappear(_ animated: Bool) {
        textView.resignFirstResponder()
        super.viewWillDisappear(animated)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        textView.becomeFirstResponder()
    }
    deinit {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

}
/*
 通知一对多
 代理一对一
 最后设置的代理对象有效
 苹果日常开发中代理监听的方式用的比较多
 代理是发生事件时直接让代理执行协议方法
 直接反向的传值
 通知是发生事件时，将通知发送给通知中心 通知中心广播通知所有的观察者，然后全部观察者执行
 如果层次嵌套的非常深 就用通知改变值
 综上代理的效率更高 通知更低
 
 */
extension WBComposeViewController:UITextViewDelegate{
    func textViewDidChange(_ textView: UITextView) {
        postButton.isEnabled = textView.hasText
        
    }
}
private extension WBComposeViewController {

    func setCustomToolBar() {
        let imageArray = ["compose_toolbar_picture","compose_mentionbutton_background","compose_trendbutton_background","compose_emoticonbutton_background","compose_add_background"]
        let selectedArray = ["compose_toolbar_picture_highlighted","compose_mentionbutton_background_highlighted","compose_trendbutton_background_highlighted","compose_emoticonbutton_background_highlighted","compose_add_background_highlighted"]
        let btnwidth = toolBar.frame.size.width/CGFloat(imageArray.count)
        for i in 0..<imageArray.count {
            let btn = UIButton(type: .custom)
            btn.frame =  CGRect(x: btnwidth*CGFloat(i), y: 0, width: btnwidth, height: toolBar.frame.size.height)
            btn .setImage(UIImage(named: imageArray[i]), for: .normal)
            btn .setImage(UIImage(named: selectedArray[i]), for: .highlighted)
            btn.tag = 200 + i
            btn.addTarget(selectedArray, action: #selector(toolBarBtnClick), for: .touchUpInside)
            toolBar.addSubview(btn)
        }
      
    }
   @objc private func toolBarBtnClick(but:UIButton){
//        print(but.tag)
//    let keyboardView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 258))
//    keyboardView.backgroundColor = UIColor.blue
    //设置键盘视图
    textView.inputView = (textView.inputView == nil) ? emoticonView : nil
    //刷新键盘视图
    textView.reloadInputViews()
    if but.tag == 200 {
        print("1")
    }else if but.tag == 201 {
        print("2")

    }else if but.tag == 202 {
        print("3")

    }else if but.tag == 203 {
        print("4")

    }else{
        print("5")

    }
}
    
    func setUPUi() {
        setUPNav()
        textView.frame = CGRect(x: 0, y: 64, width: UIScreen.cz_screenWidth(), height: UIScreen.cz_screenHeight()-45-64)
        textView.backgroundColor = UIColor.white
        textView.font = UIFont.systemFont(ofSize: 15)
        textView.textColor = UIColor.black
        textView.alwaysBounceVertical = true
        textView.keyboardDismissMode = .onDrag
        textView.delegate = self
        self.view.addSubview(textView)
                
        toolBar.frame = CGRect(x: 0, y: textView.frame.origin.y + textView.frame.size.height, width: textView.frame.size.width, height: 45)
        toolBar.backgroundColor = UIColor.lightGray
        self.view.addSubview(toolBar)
        setCustomToolBar()
        
    }
    //设置导航栏
    func  setUPNav() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", target: self, action: #selector(close))
        //设置
        postButton.frame = CGRect(x: 0, y: 0, width: 45, height: 35)
        postButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        postButton .setTitle("发布", for: .normal)
        postButton .setTitleColor(UIColor.white, for: .normal)
        postButton.setTitleColor(UIColor.lightGray, for: .disabled)
        postButton.setBackgroundImage(UIImage(named: "common_button_orange"), for: .normal)
        postButton.setBackgroundImage(UIImage(named: "common_button_orange_highlighted"), for: .highlighted)
        postButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .disabled)
        postButton.addTarget(self, action: #selector(postStatus), for: .touchUpInside)
        //设置发送按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: postButton)
        postButton.isEnabled = false
        
        let navTitleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 45))
        navigationItem.titleView = navTitleView
        
        let topLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
        topLabel.font = UIFont.systemFont(ofSize: 15)
        topLabel.textAlignment = .center
        topLabel.textColor = UIColor.black
        topLabel.text = "发微博"
        navTitleView.addSubview(topLabel)
        
        let bottomLabel = UILabel(frame: CGRect(x: 0, y: topLabel.frame.origin.y+topLabel.frame.size.height+3, width: 100, height: 15))
        bottomLabel.font = UIFont.systemFont(ofSize: 12)
        bottomLabel.textAlignment = .center
        bottomLabel.textColor = UIColor.lightGray
        bottomLabel.text = "张甜甜"
        bottomLabel.adjustsFontSizeToFitWidth = true
        navTitleView.addSubview(bottomLabel)
    }
    @objc func postStatus(){
//        print("发送")
        //获取微博文字
//        guard let  text = textView.text else {
//            return
//        }
//        //发布微博
//        WBNetworkManager.shared .postStatuses(text: text) { (json, isSuccess) in
//            print(json)
//        }
        //fixme
        //
//        let image = UIImage(named: "")
//
//        WBNetworkManager.shared.postStatuses(text: textView.text, image: image) { (json,isSuccess) in
//
//        }

        let message = "发布成功"
        SVProgressHUD .setDefaultStyle(.dark)
        SVProgressHUD.show(withStatus: message)
        //如果成功关闭当前在关闭
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
            SVProgressHUD .setDefaultStyle(.light)
            SVProgressHUD .dismiss()
            self.close()
        }
    }
}

